﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DailyComics.Common.Model;
using DailyComics.Common.Model.CachingStrategies;
using DailyComics.Common.Utilities.CachingStrategies;

namespace DailyComics.Common.Utilities.ImageProviderDecorator
{
  public class ImageProviderCacheDecorator : ImageProviderDecoratorBase
  {
    private ICachingStrategy cachingStartegy;

    public ImageProviderCacheDecorator(IImageProvider provider, ICachingStrategy strategy)
      : base(provider)
    {
      cachingStartegy = strategy ?? new NullCachingStartegy();
    }

    public override byte[] GetImage(DateTime date)
    {
      if (cachingStartegy.Contains(date))
        return cachingStartegy.Get(date);


      //TODO Handle imgdata = null 
      var imgData = base.GetImage(date);
      cachingStartegy.Add(date, imgData);

      return imgData;
    }

    public ICachingStrategy NullCachingStrategy { get; set; }
  }
}
